// $(()=>{
//     var subTotal = 0
//     $(".button_inc").on("click",function (){
//         console.log("dddd")
//         let amount = $(".quantity_1");
//         let price = $(".priceStrong")
//         let total = $(".totalStrong")
//         for (let i = 0;i<=amount.length;i++){
//             // console.log(items[i].good.price)
//             // console.log(amount[i].value)
//
//             let totalPrice = parseFloat((items[i].good.price)*(amount[i].value)).toFixed(2);
//             total[i].innerText = "￥"+totalPrice;
//             subTotal+=parseFloat(totalPrice);
//             $("#subTotal").text("￥"+ subTotal)
//             subTotal += 7;
//             $("#total").text("￥"+ subTotal)
//         }
//
//     })
//
//     console.log(items)
// })

$(()=>{
    updateData()
    function updateData(){
        console.log("1111");
        let $trs = $("#cart-table tr");
        let subTotal = 0;//小计
        $trs.each(function (){
            let $tr = $(this);
            let price = $tr.find(".priceStrong").text().trim();
            if (price.startsWith("￥")){
                price = price.substring(1);
            }
            let quantity = $tr.find(".qty2").val();
            let total = parseFloat(price) * parseInt(quantity);
            subTotal += total;
            $tr.find(".totalStrong"). text("￥"+parseFloat(total).toFixed(2))
        });
        $("#subTotal").text("￥"+subTotal);
        $("#total").text("￥" + (subTotal+15));
    }


    $("#cart-table").on("click","tr .ti-trash",function (){
        let cartItemId = $(this).closest("tr").data("id");
        let $tr = $(this).closest("tr");
        deleteById(cartItemId,function (){
            $tr.remove();
            updateData();
        })
    })

    $("#clear-cart-btn").click(function (){
        deleteById(null,function (){
            $("#cart-table").empty();
            updateData();
        })
    })
    function deleteById(id,cb){
        let data={};
        if (id!==undefined && id !== null){
            data.id = id;
        }
        let url = ctx + "/cart/delete";
        $.ajax({
            url,
            method:"delete",
            dataType:"json",
            data,
            success(resp){
                if (resp.success){
                    layer.msg("操作成功");
                }
                if (cb){
                    cb();
                }
            },error(resp){
                console.log(resp);
                layer.msg("操作失败")
            }
        })
    }

    let a = $(".goodPrice");
    let b = $(".goodAmount");
    let priceTotal = 0;
    // console.log(a)
    for (let i = 0;i<a.length;i++){
        priceTotal += a[i].value * b[i].value;
    }
    $("#cartTotal").text('￥'+priceTotal);


});

